<template>
  <AppTitle title="商品分类">
    <div class="addTypeBtn" @click="addType">添加分类</div>
  </AppTitle>
  <!-- 表格内容 -->
  <div class="goodsType-box">
    <div class="goots-type">
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="id" label="序号" />
        <el-table-column label="分类名">
        <template #default="scope">
          <el-input v-if="scope.row.isEditing" v-model="scope.row.cateName" :disabled="!scope.row.isEditing"/>
          <span v-else>{{ scope.row.cateName }}</span>
        </template>
        </el-table-column>
        <el-table-column label="是否启用">
          <template #default="scope">
            <!-- v-model="scope.row.state"接收的是布尔值 -->
            <el-switch v-model="scope.row.state" :disabled="!scope.row.isEditing"/>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button size="small"
           @click="handleEdit(scope.$index, scope.row)"
           :type="scope.row.isEditing ? 'success' : 'default'"
           >
              {{ scope.row.isEditing ? '完成' : '编辑' }}
            </el-button>
            <el-button
              size="small"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
  <!-- 分页器 -->
  <div class="demo-pagination-block">
    <el-pagination
      v-model:current-page="pagination.currentPage"
      v-model:page-size="pagination.pageSize"
      :page-sizes="[5, 10, 20, 50]"
      :size="size"
      :disabled="disabled"
      :background="background"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    </div>
  <!-- 弹窗添加 -->
  <el-dialog v-model="dialogFormVisible" title="添加分类" width="500">
    <el-form :model="addCateForm">
      <el-form-item label="分类名称" :label-width="formLabelWidth">
        <el-input v-model="addCateForm.cateName" autocomplete="off" />
      </el-form-item>
      <el-form-item label="是否启用" :label-width="formLabelWidth">
        <el-select v-model="addCateForm.state" placeholder="请选择是否启用">
          <el-option label="不启用" value="0" />
          <el-option label="启用" value="1" />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="editbtn">
          提交
        </el-button>
      </div>
    </template>
  </el-dialog>
  
</template>

<script setup>
import AppTitle from '../../components/AppTitle.vue';
import { reactive, ref, onMounted } from 'vue';
import { goodsCatelist,goodsEditcate,goodsDelcate,goodsAddcate } from '@/api/goods';
import { ElMessage } from 'element-plus';
// const dialogTableVisible = ref(false)
const dialogFormVisible = ref(false)
const total = ref(0)

// 定义表格数据
const tableData = ref([]);

// 定义分页信息
const pagination = reactive({
  currentPage: 1,
  pageSize: 5
});

// 添加分类弹窗数据
const addCateForm = reactive({
  cateName: '',
  state:''
})
const getTypeList = async () => {
    const res = await goodsCatelist(pagination);
      total.value = res.total;
       // 为每行数据添加 isEditing 字段，初始值为 false，并将 state 转换为布尔值 , state: item.state === 1
     tableData.value = res.data.map(item => ({ ...item, isEditing: false, state: item.state === 1}));
    

    
};

// 组件挂载时获取数据
onMounted(() => {
  getTypeList();
});

const handleEdit =async (index, row) => {
  if (row.isEditing) {
    // 保存修改
    // 将 state 转换为数字,并且不改变原来的数据
    const newRow = {...row };
    newRow.state = newRow.state? 1 : 0;

    let res = await goodsEditcate(newRow)
    console.log(res);
    if (res.code === 0) {
      ElMessage.success('修改成功');
      // 更新表格数据
      tableData.value[index] = {...row};
    } else {
      ElMessage.error('修改失败');
    }
  }
  // 切换编辑状态
  tableData.value[index].isEditing =!tableData.value[index].isEditing;
};

const handleDelete = async (index, row) => {
  let res = await goodsDelcate({id: row.id});
  console.log(res);
  if (res.code === 0) {
    ElMessage.success('删除成功');
    getTypeList();
    getTypeList();
  } else {
    ElMessage.error('删除失败');
  }
};
// 点击了添加分类
const addType = () => {
  // 显示弹窗
  dialogFormVisible.value = true;
}
const editbtn = async () => {
  let res = await goodsAddcate(addCateForm);
  console.log(res);
  if (res.code === 0) {
    ElMessage.success('添加成功');
    getTypeList();
    dialogFormVisible.value = false;
  } else {
    ElMessage.error('添加失败');
  }
}
// 分页器 变化处理
const handleSizeChange = () => {
  getTypeList();
};

const handleCurrentChange = () => {
  getTypeList();
}
</script>

<style lang="scss" scoped>
.goodsType-box {
  width: 100%;
  box-sizing: border-box; /* 设置盒子的 box-sizing 为 border-box */
  padding: 12px 20px; /* 设置内边距 */
}

.goots-type {
  width: 100%; /* 确保表格宽度占满父元素 */
}

.addTypeBtn {
  width: 56px;
  height: 14px;
  padding: 12px 20px;
  border: 1px solid #dcdfe6;
  color: #606266;
  font-size: 14px;
  text-align: center;
  line-height: 14px;
  margin-top: 6px;
  border-radius: 5px;
}
.addTypeBtn:hover {
  color: #409eff;
  border-color: #c6e2ff;
  background-color: #ecf5ff;
}
/* 修改 el-switch 激活状态颜色为绿色 */
// .is-checked 选中状态
:deep(.el-switch.is-checked .el-switch__core) {
  background-color: rgb(19, 206, 102);;
  border-color: rgb(19, 206, 102);
}

</style>
<style>
.el-table .cell {
  white-space: nowrap; /* 禁止换行 */
  overflow: hidden; /* 超出部分隐藏 */
  text-overflow: ellipsis; /* 超出部分显示省略号 */
}
.el-table th.el-table__cell {
  background-color: #f5f7fa;
}
.el-table td.el-table__cell {
  padding: 12px 0;
}
 /* 分页器 */
 .demo-pagination-block{
  margin: 15px 0 0 0;
  padding-left: 15px;
}
</style>